<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      v-model="visible"
      width="500px"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      @close="onClose"
    >
      <el-row :gutter="15">
        <el-form
          ref="elForm"
          :model="formData"
          :rules="rules"
          label-width="100px"
        >
          <el-col :span="24">
            <el-form-item label="生成类型" prop="type">
              <el-radio-group v-model="formData.type">
                <el-radio-button
                  v-for="(item, index) in typeOptions"
                  :key="index"
                  :label="item.value"
                  :disabled="item.disabled"
                >
                  {{ item.label }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item v-if="showFileName" label="文件名" prop="fileName">
              <el-input
                v-model="formData.fileName"
                placeholder="请输入文件名"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <template #footer>
        <div>
          <el-button @click="close"> 取消 </el-button>
          <el-button type="primary" @click="handelConfirm"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive, watch, onMounted } from "vue";

const props = defineProps({
  showFileName: Boolean,
  visible: Boolean,
});

const emit = defineEmits(["update:modelValue", "confirm"]);
const elForm = ref(null);
const visible = ref(props.visible || false);

const formData = reactive({
  fileName: undefined,
  type: "file",
});

const rules = {
  fileName: [
    {
      required: true,
      message: "请输入文件名",
      trigger: "blur",
    },
  ],
  type: [
    {
      required: true,
      message: "生成类型不能为空",
      trigger: "change",
    },
  ],
};

const typeOptions = [
  {
    label: "页面",
    value: "file",
  },
  {
    label: "弹窗",
    value: "dialog",
  },
];

const close = (e) => {
  visible.value = false;
  emit("update:modelValue", visible);
};

const handelConfirm = () => {
  elForm.value.validate((valid) => {
    if (!valid) return;
    emit("confirm", { ...formData });
    close();
  });
};

const onClose = () => {
  // 关闭对话框时的逻辑
};

// 如果有需要观察的数据，可以使用 watch
watch(
  () => props.showFileName,
  (newVal) => {
    if (props.showFileName) {
      formData.fileName = `${+new Date()}.vue`;
    }
  }
);

watch(
  () => props.visible,
  (newVal) => {
    visible.value = newVal;
  }
);

// 生命周期钩子
onMounted(() => {
  // 组件挂载后的逻辑
});
</script>

<style lang="scss" scoped></style>
